package com.yilin.tms.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.NoLoginSecurity;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.capital.TradeRecord;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrder;
import com.yilin.tms.core.entity.order.party.OrderDispatchTask;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.remote.ICapitalRemote;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.order.service.clearing.IGatherService;
import com.yilin.tms.order.service.order.IDispatchService;
import com.yilin.tms.order.service.order.IGoodsService;
import com.yilin.tms.order.service.order.IRobService;
import com.yilin.tms.order.service.order.ITransitService;
import com.yilin.tms.order.service.party.IOrderAppraiseService;
import com.yilin.tms.order.service.payment.IOrderPaymentCostService;
import com.yilin.tms.order.service.payment.IOrderRefundCostService;
import com.yilin.tms.order.service.payment.IOrderSettlementCostService;
import com.yilin.tms.order.service.payment.IServicePaymentCostService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order/task/")
public class TaskController extends BaseController {
    @Resource
    private IOrderPaymentCostService orderPaymentCostService;
    @Resource
    private IOrderSettlementCostService orderSettlementCostService;
    @Resource
    private IOrderRefundCostService orderRefundCostService;
    @Resource
    private IServicePaymentCostService servicePaymentCostService;
    @Resource
    private ICapitalRemote capitalRemote;

    /**
     * 通知支付结果
     * 服务之间通知，服务发送时，保存密钥对，公钥发送，私钥保存，接收时匹配密钥对
     * 这里无需登录，因为收到通知没有登录支持
     */
    @NoLoginSecurity
    @SkipLoginVerify
    @PostMapping(value = "updateOrderPaymentResult")
    @NotNull({"tradeRecordIds"})
    public ReturnData<Object> updateOrderPaymentResult(String[] tradeRecordIds) {
        Account loginUser = getLoginUser();
        List<TradeRecord> tradeRecordList = ParamUtil.isSuccess(capitalRemote.getTradeRecordListByIds(String.join(",", tradeRecordIds)));
        for (TradeRecord tradeRecord : tradeRecordList) {
            if (tradeRecord.getTradeType() == TradeRecord.TradeType.payment) {
                orderPaymentCostService.updateOrderPaymentResult(tradeRecord, loginUser);
            } else if (tradeRecord.getTradeType() == TradeRecord.TradeType.settlement) {
                orderSettlementCostService.updateOrderSettlementResult(tradeRecord, loginUser);
            } else if (tradeRecord.getTradeType() == TradeRecord.TradeType.refund) {
                orderRefundCostService.updateOrderRefundResult(loginUser,tradeRecord);
            }else if (tradeRecord.getTradeType() == TradeRecord.TradeType.service) {
                servicePaymentCostService.updateServicePaymentResult(loginUser,tradeRecord);
            }
        }
        return jsonView(SUCCESS_CODE, "支付单通知支付成功");
    }


}
